package com.tensquare.notice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tensquare.entity.Result;
import com.tensquare.notice.client.ArticleClient;
import com.tensquare.notice.client.UserClient;
import com.tensquare.notice.dao.TbNoticeDao;
import com.tensquare.notice.dao.TbNoticeFreshDao;
import com.tensquare.notice.pojo.TbNotice;
import com.tensquare.notice.pojo.TbNoticeFresh;
import com.tensquare.notice.service.TbNoticeService;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 文章(TbNotice)表服务实现类
 *
 * @author long
 * @since 2021-07-17 16:11:23
 */
@Service("tbNoticeService")
public class TbNoticeServiceImpl implements TbNoticeService {
    @Resource
    private TbNoticeDao tbNoticeDao;
    @Resource
    private TbNoticeFreshDao tbNoticeFreshDao;
    @Autowired
    private UserClient userClient;
    @Autowired
    private ArticleClient articleClient;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void insert(TbNotice notice) {
        String id = String.valueOf(idWorker.nextId());
        notice.setId(id);
        notice.setState("0");
        notice.setCreatetime(new Date());
        tbNoticeDao.insert(notice);

        //使用mq代替新消息的发送
        //添加待推送消息
//        TbNoticeFresh noticeFresh = new TbNoticeFresh();
//        noticeFresh.setId(id);
//        noticeFresh.setNoticeid(id);
//        noticeFresh.setUserid(notice.getReceiverid());
//        tbNoticeFreshDao.insert(noticeFresh);
    }

    @Override
    public void delete(String id) {
        tbNoticeDao.deleteById(id);
    }

    @Override
    public void update(TbNotice notice) {
        tbNoticeDao.updateById(notice);
    }

    @Override
    public TbNotice findById(String id) {
        TbNotice tbNotice = tbNoticeDao.selectById(id);
        getNoticeInfo(tbNotice);
        return tbNotice;
    }

    /**
     * 按条件分页查询信息
     *
     * @param map
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<TbNotice> findByPage(Map<String, Object> map, int page, int size) {
        QueryWrapper<TbNotice> wrapper = new QueryWrapper<>();
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            wrapper.eq(null != map.get(s), s, map.get(s));
        }
        wrapper.eq("isdelete", 0);
        Page<TbNotice> tbNoticePage = new Page<>(page, size);
        tbNoticeDao.selectPage(tbNoticePage, wrapper);
        for (TbNotice notice : tbNoticePage.getRecords()) {
            getNoticeInfo(notice);
        }
        return tbNoticePage;
    }

    @Override
    public void freshDelete(String id) {
        tbNoticeFreshDao.deleteById(id);
    }

    @Override
    public Page<TbNoticeFresh> findFreshByPage(String userId, int page, int size) {
        QueryWrapper<TbNoticeFresh> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        Page<TbNoticeFresh> tbNoticeFreshPage = new Page<>();
        tbNoticeFreshDao.selectPage(tbNoticeFreshPage, wrapper);
        return tbNoticeFreshPage;
    }

    /**
     * 查询消息相关的数据
     *
     * @param notice 要完善的消息
     */
    private void getNoticeInfo(TbNotice notice) {
        Result userResult = userClient.findById(notice.getReceiverid());
        HashMap userData = (HashMap) userResult.getData();
        notice.setOperatorname((String) userData.get("nickname"));

        if ("article".equals(notice.getTargettype())) {
            Result articleResult = articleClient.findById(notice.getTargetid());
            HashMap articleData = (HashMap) articleResult.getData();
            notice.setTargetName((String) articleData.get("title"));
        }
    }
}
